package com.brush.brushcommon.utils;


import com.brush.brushcommon.vo.basic.CategoryVo;
import com.brush.brushcommon.common.basic.pojo.Category;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CategoryRecursion {

    /**
     * 递归分类
     *
     * @param categoryList
     */
    public static List<CategoryVo> recursion(List<Category> categoryList) {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (Category item : categoryList) {
            if (item.getParentId().equals("0")) {
             log.info("----入口-----");
                categoryVoList.add(findChildren(item, categoryList));
            }
        }
        return categoryVoList;
    }

    private static CategoryVo findChildren(Category item, List<Category> categoryList) {
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCategoryName(item.getCategoryName());
        categoryVo.setId(item.getId());
        categoryVo.setGmtCreate(item.getGmtCreate());
        categoryVo.setGmtFounder(item.getGmtFounder());
        categoryVo.setGmtModified(item.getGmtModified());
        categoryVo.setParentId(item.getParentId());
        for (Category category : categoryList) {
            if(categoryVo.getChildren()==null){
                categoryVo.setChildren(new ArrayList<>());
            }
            if (category.getParentId().equals(item.getId())) {
                categoryVo.getChildren().add(findChildren(category, categoryList));
            }
        }
        return categoryVo;
    }






}
